/**
 * 作者:李杰
 * email: maillijie@gmail.com
 */
package com.javainfo.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;
import org.apache.struts2.rest.RestActionSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.javainfo.dto.PageDto;
import com.javainfo.entity.Attachments;
import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings({"unchecked"})
public abstract class JavaInfoActionSupport extends RestActionSupport implements
		ModelDriven<Object>, ServletResponseAware, FileUploadSupport,
		SessionAware, ServletRequestAware {

	/** serialVersionUID */
	private static final long serialVersionUID = 1L;
	
    private static final Logger logger = LoggerFactory
			.getLogger(JavaInfoActionSupport.class);

    private ServiceFactory serviceFactory;
	public ServiceFactory getServiceFactory() {
		return serviceFactory;
	}
	public void setServiceFactory(ServiceFactory serviceFactory) {
		this.serviceFactory = serviceFactory;
	}

	private Map session;
	private HttpServletResponse response;
	private HttpServletRequest request;
	private String id;
	public void setSession(Map session) {this.session = session;}
	public HttpServletResponse getResponse() {return response;}
	public void setServletResponse(HttpServletResponse response) {this.response = response;}
	public HttpServletRequest getRequest() {return request;}
	public void setServletRequest(HttpServletRequest request) {this.request = request;}
	public Map getSession() {return session;}
	public void setId(String id) {this.id = id;}
	public String getId() {return id;}
	
	public static final String EDIT_NEW = "editNew";
	public static final String SHOW = "show";
	public static final String INDEX = "index";
	public static final String EDIT = "edit";
	public static final String DELETE_CONFIRM = "deleteConfirm";
	
	private Object model = null;
	public Object getModel() {return model;}
	protected void setModel(Object model) {this.model = model;}
	
	public HttpHeaders show(){
		return new DefaultHttpHeaders(SHOW);
	};
	public HttpHeaders edit(){
		return new DefaultHttpHeaders(EDIT);
	};
	public HttpHeaders editNew(){
		return new DefaultHttpHeaders(EDIT_NEW);
	};
	public HttpHeaders create(){
		return new DefaultHttpHeaders(INDEX);
	};
	public HttpHeaders update(){
		return new DefaultHttpHeaders(INDEX);
	};
	public HttpHeaders destroy(){
		return new DefaultHttpHeaders(INDEX);
	};
	
	
	/** 文件上传之后，存到文件系统的路径 */
	private String savePath = null;
	/** 上传的文件 */
	private File[] uploadFiles = null;
	/** 用String数组来封装多个上传文件名 */
	private String[] uploadFilesFileName = null;
	/** 用String数组来封装多个上传文件类型 */
	private String[] uploadFilesContentType = null;
	/** 文件上传之后是到文件系统还是数据库 */
	private String fileSaveType = null;
	/** 文件上传的注释 */
	private String[] fileComment = null;
	
	public String getSavePath() {return savePath;}
	public void setSavePath(String savePath) {this.savePath = savePath;}
	public File[] getUploadFiles() {return uploadFiles;}
	public void setUploadFiles(File[] uploadFiles) {this.uploadFiles = uploadFiles;}
	public String[] getUploadFilesFileName() {return uploadFilesFileName;}
	public void setUploadFilesFileName(String[] uploadFilesFileName) {this.uploadFilesFileName = uploadFilesFileName;}
	public String[] getUploadFilesContentType() {return uploadFilesContentType;}
	public void setUploadFilesContentType(String[] uploadFilesContentType) {this.uploadFilesContentType = uploadFilesContentType;}
	public String getFileSaveType() {return fileSaveType;}
	public void setFileSaveType(String fileSaveType) {this.fileSaveType = fileSaveType;}
	public String[] getFileComment() {return fileComment;}
	public void setFileComment(String[] fileComment) {this.fileComment = fileComment;}
	
	@SuppressWarnings("finally")
	protected HttpHeaders urlForward(String url){
		try {
			logger.info("程序内部转向:"+getRequest().getContextPath()+url);
			getResponse().sendRedirect(getRequest().getContextPath()+url);
		} catch (IOException e) {
			logger.error("转发失败!"+e.getMessage());
		}finally{
			return null;
		}
	}
	
	/**
	 * 画面获取图片附件
	 * @return
	 * @throws Exception
	 */
	public String dynamicImage() throws Exception {
		HttpServletResponse response = null;
		ServletOutputStream out = null;
		InputStream in = null;
		byte[] image = null;
		try {
			response = ServletActionContext.getResponse();
			// 二进制输出流
			response.setContentType("multipart/form-data");
			// 得到输出流
			out = response.getOutputStream();
			// 得到Blob类实例，使用.getBinaryStream()得到输入流。
			// 这里的image就是blob类
			Attachments attachments = serviceFactory.getAttachmentsService().getById(Integer.valueOf(id));
			image = attachments.getContent();
			out.write(image);
			// 强制刷新输出流
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (out != null)
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			image = null;
		}
		return null;
	}
	
	private PageDto pageDto;
	public PageDto getPageDto() {return pageDto;}
	public void setPageDto(PageDto pageDto) {this.pageDto = pageDto;}
	
	private Attachments attachments;
	public Attachments getAttachments() {return attachments;}
	public void setAttachments(Attachments attachments) {this.attachments = attachments;}
	
	private String redirectUrl;
	public String getRedirectUrl() {return redirectUrl;}
	public void setRedirectUrl(String redirectUrl) {this.redirectUrl = redirectUrl;}

}
